package com.ecommerce.dal.dao.impl;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.ecommerce.dal.dao.IMembershipCardDAO;
import com.ecommerce.dal.entity.MembershipCard;
import com.ecommerce.dal.util.CustomHibernateDAOSupport;

@Repository("membershipCardDAO")
public class MembershipCardDAO extends CustomHibernateDAOSupport implements
		IMembershipCardDAO {

	@Override
	public void save(MembershipCard membershipCard) {
		getHibernateTemplate().save(membershipCard);

	}

	@Override
	public void update(MembershipCard membershipCard) {
		getHibernateTemplate().update(membershipCard);

	}

	@Override
	public void delete(MembershipCard membershipCard) {
		getHibernateTemplate().delete(membershipCard);

	}

	@Override
	public MembershipCard findByMembershipCardId(long membershipCardId) {
		List list = getHibernateTemplate()
				.find("from MembershipCard membershipCard where membershipCardId=?",
						membershipCardId);
		return (MembershipCard) ((list != null && list.size()>0) ?  list.get(0) : null) ;
	}

	@Override
	public List<MembershipCard> findAllMembershipCards() {
		List list = getHibernateTemplate().find("from MembershipCard");
		return ((list != null && list.size()>0) ?  list : null) ;

	}

	@Override
	public double findMembershipCardPrice(long membershipCardId) {
		List list = getHibernateTemplate()
				.find("select membershipCard.cardPrice from MembershipCard membershipCard where membershipCardId=?",
						membershipCardId);
		return (Double)((list != null && list.size()>0) ?  list.get(0) : null) ;
	}

	/*
	 * check to see if membershipCardId already exists in the system or valid
	 * MembershipCard return true if already in the system , false otherwise
	 */
	@Override
	public boolean checkMembershipCard(long membershipCardId) {
		List list = getHibernateTemplate()
				.find("select count(membershipCard) from MembershipCard membershipCard where membershipCardId = ?",
						membershipCardId);
		return !((Double) (list.get(0))).equals(0) ? true : false;
	}

}
